package com.pro.staffmanage.Interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Haotian Cui
 * @Date: 2022/7/23 - 07 - 23 - 17:46
 * @Description: com.pro.staffmanage.config
 * @Project: staffmanage
 * @version: 1.0
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //  After successful login, there should be user's session
        Object loginUser = request.getSession().getAttribute("user");
        //  Go directly to the home page (templatesdashboard.html) without logging in
        if (loginUser == null){
            request.setAttribute("error","没有权限, 请先登录");
            request.getRequestDispatcher("/")
                    .forward(request, response);
            return false;
        }
        return true;
    }

}
